package com.yxw.live_vod_boot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_boot.dto.fans.FansQueryDTO;
import com.yxw.live_vod_boot.dto.fans.ShopFollowQueryDTO;
import com.yxw.live_vod_boot.dto.follow.FollowStateQueryDTO;
import com.yxw.live_vod_boot.entity.YxFansEntity;
import com.yxw.live_vod_boot.mapper.FansMapper;
import com.yxw.live_vod_boot.mapper.FollowMapper;
import com.yxw.live_vod_boot.service.FansService;
import com.yxw.live_vod_boot.service.FollowService;
import com.yxw.live_vod_boot.utils.AssertUtil;
import com.yxw.live_vod_boot.utils.PageUtils;
import com.yxw.live_vod_boot.utils.UserUtil;
import com.yxw.live_vod_boot.vo.*;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FansServiceImpl extends ServiceImpl<FansMapper, YxFansEntity> implements FansService {

    @Autowired
    private FansMapper fansMapper;

    @Autowired
    private FollowService followService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 店铺侧-展示自己的粉丝列表
     */
    @Override
    public PageVO<FansListVO> getFansListByShopId(Long shopId, FansQueryDTO dto) {
        Long userId = userUtil.getLoginUserId();
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        AssertUtil.isNotNull(shopId, ErrorCodeEnum.ILLEGAL_OPERATION);
        Page<YxFansEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<FansListVO> list = fansMapper.getFansListByShopId(page, shopId);
        for (FansListVO listVO : list) {
            FollowStateQueryDTO followStateQueryDTO = new FollowStateQueryDTO();
            followStateQueryDTO.setFollowerType(0);
            followStateQueryDTO.setFollowerShopId(shopId);
            followStateQueryDTO.setFolloweeId(listVO.getUserId());
            followStateQueryDTO.setFolloweeType(listVO.getUserType());
            if (null != listVO.getShopId()) {
                followStateQueryDTO.setFolloweeShopId(listVO.getShopId());
            }
            FollowStateVO state = followService.checkFollowStateForUser(userId, followStateQueryDTO);
            if (null != state && null != state.getFollowState()) {
                if (Boolean.TRUE.equals(state.getFollowState())) {
                    listVO.setIsFollowed(0);
                } else {
                    listVO.setIsFollowed(1);
                }
            }
        }
        return PageUtils.get(page, list);
    }

    /**
     * 店铺侧-展示自己的关注列表
     */
    @Override
    public PageVO<ShopFollowListVO> getShopFollowListByShopId(Long shopId, ShopFollowQueryDTO dto) {
        Long userId = userUtil.getLoginUserId();
        AssertUtil.isNotNull(userId, ErrorCodeEnum.COLLECTION_USER_ID_ERROR);
        AssertUtil.isNotNull(shopId, ErrorCodeEnum.ILLEGAL_OPERATION);
        Page<YxFansEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<ShopFollowListVO> list = fansMapper.getShopFollowListByShopId(page, shopId);
        for (ShopFollowListVO listVO : list) {
            FollowStateQueryDTO followStateQueryDTO = new FollowStateQueryDTO();
            followStateQueryDTO.setFollowerType(listVO.getUserType());
            followStateQueryDTO.setFolloweeId(userId);
            followStateQueryDTO.setFolloweeShopId(shopId);
            followStateQueryDTO.setFolloweeType(0);
            if (null != listVO.getShopId()) {
                followStateQueryDTO.setFollowerShopId(listVO.getShopId());
            }
            FollowStateVO state = followService.checkFollowStateForUser(userId, followStateQueryDTO);
            if (null != state && null != state.getFollowState()) {
                if (Boolean.TRUE.equals(state.getFollowState())) {
                    listVO.setIsFollowed(0);
                } else {
                    listVO.setIsFollowed(1);
                }
            }
        }
        return PageUtils.get(page, list);
    }

    /**
     * 用户侧-展示自己的粉丝列表
     */
    @Override
    public PageVO<FansListVO> getFansListForUser(Long userId, FansQueryDTO dto) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.ILLEGAL_OPERATION);
        Page<YxFansEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        List<FansListVO> list = fansMapper.getFansListForUser(page, userId);
        for (FansListVO listVO : list) {
            FollowStateQueryDTO followStateQueryDTO = new FollowStateQueryDTO();
            followStateQueryDTO.setFollowerType(1);
            followStateQueryDTO.setFolloweeId(listVO.getUserId());
            followStateQueryDTO.setFolloweeType(listVO.getUserType());
            if (null != listVO.getShopId()) {
                followStateQueryDTO.setFolloweeShopId(listVO.getShopId());
            }
            FollowStateVO state = followService.checkFollowStateForUser(userId, followStateQueryDTO);
            if (null != state && null != state.getFollowState()) {
                if (Boolean.TRUE.equals(state.getFollowState())){
                    listVO.setIsFollowed(0);
                }else {
                    listVO.setIsFollowed(1);
                }
            }
        }
        return PageUtils.get(page, list);
    }

    /**
     * 店铺侧-查询自己的关注数量和粉丝数量
     */
    @Override
    public FansNumberAndFollowNumberVO getFansNumberAndFollowNumberForShop(Long shopId) {
        AssertUtil.isNotNull(shopId, ErrorCodeEnum.ILLEGAL_OPERATION);
        FansNumberAndFollowNumberVO result = fansMapper.getFansNumberAndFollowNumberForShop(shopId);
        FansNumberAndFollowNumberVO vo = new FansNumberAndFollowNumberVO();
        if (null != result && null != result.getFansNumber() && null != result.getFollowNumber()) {
            vo.setFansNumber(result.getFansNumber());
            vo.setFollowNumber(result.getFollowNumber());
        }
        return vo;
    }

    /**
     * 用户侧-查询自己的关注数量和粉丝数量
     */
    @Override
    public FansNumberAndFollowNumberVO getFansNumberAndFollowNumberForUser(Long userId) {
        AssertUtil.isNotNull(userId, ErrorCodeEnum.ILLEGAL_OPERATION);
        FansNumberAndFollowNumberVO result = fansMapper.getFansNumberAndFollowNumberForUser(userId);
        FansNumberAndFollowNumberVO vo = new FansNumberAndFollowNumberVO();
        if (null != result && null != result.getFansNumber() && null != result.getFollowNumber()) {
            vo.setFansNumber(result.getFansNumber());
            vo.setFollowNumber(result.getFollowNumber());
        }
        return vo;
    }
}